<template>
	<view class="add-car-info">
		<!-- 新增车辆 -->
		<u-navbar leftIconSize="34rpx" autoBack :placeholder="true" bgColor="#ffffff">
			<view class="" slot="center">
				<text class="c-000 pf-sc fw-600" style="font-size:  rpx;">车辆管理</text>
			</view>
		</u-navbar>
		<view
			class="set-data-container w-750 b-box d-flex flex-c ali-i-c  bg-ff po-re"
			style="padding:20rpx 36rpx 0;"
			:style="{ height: containerHeight }"
		>
			<view style="margin-bottom: 22rpx;" class="b-box w-100">
				<view
					class="d-flex flex-r ali-i-c just-c b-box w-100 bg-ff"
					style="border-bottom: 2rpx solid #EEEEEE;height: 112rpx;"
				>
					<text
						class="pf-sc fw-400"
						style="font-size: 30rpx;color: rgba(0,0,0,0.9000);margin-right: 54rpx;"
					>
						车牌
					</text>
					<u-input
						type="text"
						border="none"
						placeholder="请填写车牌号"
						v-model="car.carno"
						placeholderStyle="{color:#999999;fontWeight: 400;fontsize:30px;fontfamily:PingFang SC-Regular, PingFang SC"
						:customStyle="{ color: 'rgba(0,0,0,0.9000)', font: '400 30rpx PingFang SC' }"
					></u-input>
				</view>
				<view
					class="d-flex flex-r ali-i-c just-c b-box w-100 bg-ff po-re"
					style="border-bottom: 2rpx solid #EEEEEE;height: 112rpx;"
				>
					<text
						class="pf-sc fw-400"
						style="font-size: 30rpx;color: rgba(0,0,0,0.9000);margin-right: 54rpx;"
					>
						车型
					</text>
					<u-input
						type="text"
						v-model="car.cartype"
						border="none"
						placeholder="请输入车型"
						placeholderStyle="{color:#999999;fontWeight: 400;fontsize:30px;fontfamily:PingFang SC-Regular, PingFang SC"
						:customStyle="{ color: 'rgba(0,0,0,0.9000)', font: '400 30rpx PingFang SC' }"
					></u-input>
				</view>
				<view
					class="d-flex flex-r ali-i-c just-c b-box w-100 bg-ff po-re"
					style="border-bottom: 2rpx solid #EEEEEE;height: 112rpx;"
				>
					<text
						class="pf-sc fw-400"
						style="font-size: 30rpx;color: rgba(0,0,0,0.9000);margin-right: 54rpx;"
					>
						颜色
					</text>
					<u-input
						type="text"
						v-model="car.carcolor"
						border="none"
						placeholder="请输入颜色"
						placeholderStyle="{color:#999999;fontWeight: 400;fontsize:30px;fontfamily:PingFang SC-Regular, PingFang SC"
						:customStyle="{ color: 'rgba(0,0,0,0.9000)', font: '400 30rpx PingFang SC' }"
					></u-input>
				</view>
				<view
					class="d-flex flex-r ali-i-c just-c b-box w-100 bg-ff"
					style="border-bottom: 2rpx solid #EEEEEE;height: 112rpx;"
				>
					<text
						class="pf-sc fw-400"
						style="font-size: 30rpx;color: rgba(0,0,0,0.9000);margin-right: 24rpx;"
					>
						联系人
					</text>
					<u-input
						type="text"
						v-model="car.name"
						border="none"
						placeholder="请填写联系人姓名"
						placeholderStyle="{color:#999999;fontWeight: 400;fontsize:30px;fontfamily:PingFang SC-Regular, PingFang SC"
						:customStyle="{ color: 'rgba(0,0,0,0.9000)', font: '400 30rpx PingFang SC' }"
					></u-input>
				</view>
				<view
					class="d-flex flex-r ali-i-c just-c b-box w-100 bg-ff"
					style="border-bottom: 2rpx solid #EEEEEE;height: 112rpx;"
				>
					<text
						class="pf-sc fw-400"
						style="font-size: 30rpx;color: rgba(0,0,0,0.9000);margin-right: 24rpx;"
					>
						手机号
					</text>
					<u-input
						type="number"
						v-model="car.mobile"
						border="none"
						placeholder="请填写手机号"
						placeholderStyle="{color:#999999;fontWeight: 400;fontsize:30px;fontfamily:PingFang SC-Regular, PingFang SC"
						:customStyle="{ color: 'rgba(0,0,0,0.9000)', font: '400 30rpx PingFang SC' }"
					></u-input>
				</view>
			</view>
			<!--  -->
			<view
				class="d-flex flex-r ali-i-c just-sw b-box w-100 bg-ff"
				style="height: 112rpx;margin-bottom: 60rpx;"
				v-if="type==4"
			>
				<text
					class="pf-sc fw-400"
					style="font-size: 30rpx;color: rgba(0,0,0,0.9000);margin-right: 24rpx;"
				>
					设为默认车辆
				</text>
				<u-switch
					v-model="car.isdefault"
					@change="AddressDefaultChange"
					size="44"
					activeColor="#2AC128"
				></u-switch>
			</view>
			<!-- btn -->
			<view style="height: 206rpx;" class="w-100 b-box d-flex flex-c ali-i-c just-sw">
				<u-button
					:throttleTime="700"
					@click="AddCarInfo"
					:customStyle="{
						height: '88rpx',
						width: '670rpx',
						font: '500 32rpx PingFang SC',
						color: '#ffffff',
						borderWidth: '0rpx',
						background: 'linear-gradient(180deg, #4885C5 0%, #3B52A6 100%)',
						borderRadius: '44rpx'
					}"
					shape="square"
					text="保存"
				></u-button>
				<u-button
					:throttleTime="700"
					@click="DeleteCarInfo(3)"
					plain
					:customStyle="{
						height: '88rpx',
						width: '670rpx',
						font: '500 32rpx PingFang SC',
						color: '#333333',
						border: '2rpx solid #ECECEC',
						borderRadius: '44rpx'
					}"
					shape="square"
					text="删除"
				></u-button>
			</view>
		</view>
	</view>
</template>

<script>
import { getcarlist } from '@/common/api.js';
import { _containerHeight } from '@/utils/GetSys.js';
import Unavbar from '../components/uview-ui/components/u-navbar/u-navbar';
import Ubutton from '../components/uview-ui/components/u-button/u-button';
import uInput from '../components/uview-ui/components/u-input/u-input';
import uSwitch from '../components/uview-ui/components/u-switch/u-switch';
export default {
	components: {
		'u-navbar': Unavbar,
		'u-button': Ubutton,
		'u-input': uInput,
		'u-switch': uSwitch
	},
	data() {
		return {
			containerHeight: '',
			car:uni.getStorageSync('car') ? uni.getStorageSync('car') :   {
				carno: '',
				cartype: '',
				carcolor: '',
				name: '',
				mobile: '',
				isdefault: false
			},
			id: null, //保存当前用户传过来的车辆id通过id来删除对应的车辆
			type: null,
			carList: [],
			typecar: '', //用户下单没有车辆的时候跳转来的数据
		};
	},
	methods: {
		AddressDefaultChange(e) {
			//是否设置为默认地址
			this.car.isDefault = e;
		},
		AddCarInfo() {
			//当type等于2的时候走这个请求 否则等于4走编辑的请求
			// console.log('我是type',this.type);
			if (this.type === 2) {
				let params = {
					token: uni.getStorageSync('usertoken'),
					name: this.car.name,
					mobile: this.car.mobile,
					carno: this.car.carno,
					type: this.type,
					cartype: this.car.cartype,
					carcolor: this.car.carcolor
				};
				//先验证在发请求
				if (
					this.car.carno === '' ||
					this.car.cartype === '' ||
					this.car.carcolor === '' ||
					this.car.name === '' ||
					this.car.mobile === ''
				) {
					uni.showToast({
						title: '请填写完整的车辆信息',
						icon: 'error'
					});
					return;
				}
				let regphone = /^1[3-8]\d{9}$/;
				if (!regphone.test(this.car.mobile)) {
					uni.showToast({
						title: '请填写正确的手机号',
						icon: 'error'
					});
					return;
				}
				getcarlist(params).then(res => {
					if (res.data.code === 1) {
						uni.showToast({
							title: '添加成功',
							icon: 'success',
							success() {
								setTimeout(() => {
									uni.navigateBack({});
								}, 1000);
							}
						});
					}
				});
			} else {
				let params = {
					token: uni.getStorageSync('usertoken'),
					name: this.car.name,
					mobile: this.car.mobile,
					carno: this.car.carno,
					type: this.type,
					cartype: this.car.cartype,
					carcolor: this.car.carcolor,
					id: this.id,
					isdefult: this.car.isDefault ? 1 : 0
				};
				//先验证在发请求
				if (
					this.car.carno === '' ||
					this.car.cartype === '' ||
					this.car.carcolor === '' ||
					this.car.name === '' ||
					this.car.mobile === ''
				) {
					uni.showToast({
						title: '请填写完整的车辆信息',
						icon: 'error'
					});
					return;
				}
				let regphone = /^1[3-8]\d{9}$/;
				if (!regphone.test(this.car.mobile)) {
					uni.showToast({
						title: '请填写正确的手机号',
						icon: 'error'
					});
					return;
				}
				getcarlist(params).then(res => {
					if (res.data.code === 1) {
						uni.showToast({
							title: '车辆信息修改成功',
							icon: 'success',
							success() {
								setTimeout(() => {
									uni.navigateBack({});
								}, 1000);
							}
						});
					}
				});
			}

			//当type等于2的时候走这个请求 否则等于4走编辑的请求
			// console.log('我是type',this.type);
			if (this.typecar == 2) {
				let params = {
					token: uni.getStorageSync('usertoken'),
					name: this.car.name,
					mobile: this.car.mobile,
					carno: this.car.carno,
					type: 2,
					cartype: this.car.cartype,
					carcolor: this.car.carcolor
				};
				//先验证在发请求
				if (
					this.car.carno === '' ||
					this.car.cartype === '' ||
					this.car.carcolor === '' ||
					this.car.name === '' ||
					this.car.mobile === ''
				) {
					uni.showToast({
						title: '请填写完整的车辆信息',
						icon: 'error'
					});
					return;
				}
				let regphone = /^1[3-8]\d{9}$/;
				if (!regphone.test(this.car.mobile)) {
					uni.showToast({
						title: '请填写正确的手机号',
						icon: 'error'
					});
					return;
				}
				getcarlist(params).then(res => {
					if (res.data.code === 1) {
						uni.showToast({
							title: '添加成功',
							icon: 'success',
							success() {
								setTimeout(() => {
									uni.navigateBack({});
								}, 1000);
							}
						});
					}
				});
			}
		},

		//这是删除
		DeleteCarInfo(index) {
			let params = {
				token: uni.getStorageSync('usertoken'),
				type: index,
				id: this.id
			};
			console.log(this.id);
			getcarlist(params).then(res => {
				console.log(res);
				if (res.data.code === 1) {
					uni.showToast({
						title: '删除成功',
						icon: 'success',
						success() {
							setTimeout(() => {
								uni.navigateBack({});
							}, 1000);
						}
					});
				}
			});
		}
	},
	onLoad(options) {
		this.containerHeight = _containerHeight();
		this.id = parseInt(options.id);
		this.type = parseInt(options.type);
		this.typecar = options.typecar;
		console.log(options);
	}
};
</script>

<style lang="scss">
.add-car-info {
}
</style>
